package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.dao.PsychoanalysisDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.PT;
import com.itheima.pojo.Psychoanalysis;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 心理测评
 */
@Service(version = "1.0.0", interfaceClass = PsychoanalysisService.class)
@Transactional
public class PsychoanalysisServiceImpl implements PsychoanalysisService {

    @Resource
    private PsychoanalysisDao psychoanalysisDao;

    @Override
    public void update(Psychoanalysis psychoanalysis) {


        psychoanalysisDao.update(psychoanalysis);
    }

    @Override
    public PT findById(Integer id) {
        return psychoanalysisDao.findById(id);
    }


    @Override
    public void add(Psychoanalysis psychoanalysis) throws ParseException {
        //   List<Psychoanalysis> psList = getMap(psychoanalysis);
        List<PT> ptList = psychoanalysis.getPtList();
        Integer sum = 0;
        for (PT pt : ptList) {
            try {
                Integer score = pt.getScore();
                sum += score;
            } catch (Exception e) {
                sum+=0;
            }
        }
        if (sum <= 50) {
            psychoanalysis.setSomatization("正常");
            psychoanalysis.setOCD("正常");
            psychoanalysis.setIS("正常");
            psychoanalysis.setHesitate("正常");
            psychoanalysis.setAnxiety("正常");
            psychoanalysis.setHostility("正常");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        } else if (sum > 50 && sum <= 61) {
            psychoanalysis.setSomatization("一级");
            psychoanalysis.setOCD("正常");
            psychoanalysis.setIS("正常");
            psychoanalysis.setHesitate("正常");
            psychoanalysis.setAnxiety("正常");
            psychoanalysis.setHostility("正常");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        } else if (sum > 61 && sum <= 70) {
            psychoanalysis.setSomatization("二级");
            psychoanalysis.setOCD("一级");
            psychoanalysis.setIS("正常");
            psychoanalysis.setHesitate("正常");
            psychoanalysis.setAnxiety("正常");
            psychoanalysis.setHostility("正常");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        }
        else if(sum > 71 && sum <= 90) {
            psychoanalysis.setSomatization("三级");
            psychoanalysis.setOCD("二级");
            psychoanalysis.setIS("一级");
            psychoanalysis.setHesitate("正常");
            psychoanalysis.setAnxiety("正常");
            psychoanalysis.setHostility("正常");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        }else if (sum > 91 && sum <= 100) {
            psychoanalysis.setSomatization("四级");
            psychoanalysis.setOCD("三级");
            psychoanalysis.setIS("二级");
            psychoanalysis.setHesitate("一级");
            psychoanalysis.setAnxiety("正常");
            psychoanalysis.setHostility("正常");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        } else if (sum > 101 && sum <= 110) {
            psychoanalysis.setSomatization("五级");
            psychoanalysis.setOCD("四级");
            psychoanalysis.setIS("三级");
            psychoanalysis.setHesitate("二级");
            psychoanalysis.setAnxiety("一级");
            psychoanalysis.setHostility("正常");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        } else if (sum > 111 && sum <= 120) {
            psychoanalysis.setSomatization("六级");
            psychoanalysis.setOCD("五级");
            psychoanalysis.setIS("四级");
            psychoanalysis.setHesitate("三级");
            psychoanalysis.setAnxiety("二级");
            psychoanalysis.setHostility("一级");
            psychoanalysis.setPA("正常");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        } else if (sum > 121 && sum <= 130) {
            psychoanalysis.setSomatization("七级");
            psychoanalysis.setOCD("六级");
            psychoanalysis.setIS("五级");
            psychoanalysis.setHesitate("四级");
            psychoanalysis.setAnxiety("三级");
            psychoanalysis.setHostility("二级");
            psychoanalysis.setPA("一级");
            psychoanalysis.setParanoid("正常");
            psychoanalysis.setPsychoticism("正常");
        }else if (sum > 131 && sum <= 140) {
            psychoanalysis.setSomatization("八级");
            psychoanalysis.setOCD("七级");
            psychoanalysis.setIS("六级");
            psychoanalysis.setHesitate("五级");
            psychoanalysis.setAnxiety("四级");
            psychoanalysis.setHostility("三级");
            psychoanalysis.setPA("二级");
            psychoanalysis.setParanoid("一级");
            psychoanalysis.setPsychoticism("正常");
        }else if (sum > 141 && sum <= 150) {
            psychoanalysis.setSomatization("九级");
            psychoanalysis.setOCD("八级");
            psychoanalysis.setIS("七级");
            psychoanalysis.setHesitate("六级");
            psychoanalysis.setAnxiety("五级");
            psychoanalysis.setHostility("四级");
            psychoanalysis.setPA("三级");
            psychoanalysis.setParanoid("二级");
            psychoanalysis.setPsychoticism("一级");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        Date date = sdf.parse(time);
        psychoanalysis.setPED(date);
        System.out.println("date = " + date);
        psychoanalysisDao.add(psychoanalysis);

    }


    // 删除数据
    @Override
    public void delete(int id) {
        psychoanalysisDao.delete(id);
    }

    // 查看心理测评试题
    @Override
    public List<PT> findAll() {

        return psychoanalysisDao.findAll();
    }

    @Override
    public PageResult findPage(int pageNum, int pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //调用dao层查询语句
        Page page = psychoanalysisDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(), page.getResult());
    }
}
